﻿using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Cache;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
//using NHibernate.Cfg;


namespace FluentlyDo
{
    public class SessionManager
    {
       
        public void BuildSchema()
        {
            Configuration cfg = new Configuration().Configure();
            Configuration config =
                Fluently.Configure(cfg)
                    .Database
                    (
                    MsSqlConfiguration.MsSql2008
                        .ConnectionString(conn => conn.FromConnectionStringWithKey("FluentConnection"))
                        .ProxyFactoryFactory(
                        "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle")
                        .UseOuterJoin()
                        .Cache(c =>
                               c.UseQueryCache()
                                   .ProviderClass<HashtableCacheProvider>()
                                   .UseMinimalPuts()
                        )
                    )
                    .Mappings(m =>
                              m.FluentMappings.AddFromAssembly((typeof (SessionManager).Assembly)))
                    .Mappings(MS => MS.HbmMappings.AddFromAssembly(typeof (SessionManager).Assembly))
                    .BuildConfiguration();


            new SchemaExport(config)
                .Create(false, true);
        }

        
  
        public ISessionFactory CreateSessionFactory()
        {
            Configuration cfg = new Configuration().Configure();
            ISessionFactory sessionFactory =
                Fluently.Configure(cfg)
                    .Database
                    (
                    MsSqlConfiguration.MsSql2008
                        .ConnectionString(conn => conn.FromConnectionStringWithKey("FluentConnection"))
                      
                        .ProxyFactoryFactory(
                        "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle")
                        .UseOuterJoin()
                        .Cache(c =>
                               c.UseQueryCache()
                                   .ProviderClass<HashtableCacheProvider>()
                                   .UseMinimalPuts()
                        )
                    )
                    .Mappings(m =>
                              m.FluentMappings.AddFromAssembly((typeof (SessionManager).Assembly))
                    )
                    .Mappings(MS => MS.HbmMappings.AddFromAssembly(typeof (SessionManager).Assembly))
                    .BuildSessionFactory();

            return sessionFactory;
        }
    }
}